﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.DocTag"/>.
    /// </summary>
    public class DocTagVM : DocCommentVM
    {
        #region /* STATICS */

        internal static new void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(DocTag),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new DocTagVM((DocTag)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.DocTag"/>.
        /// </summary>
        protected DocTagVM(DocTag docTag, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(docTag, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.DocTag"/> model.
        /// </summary>
        public DocTag DocTag
        {
            get { return (DocTag)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderStart(CodeRenderer renderer, RenderFlags flags)
        {
            if (!DocTag.MissingStartTag)
            {
                // For unrecognized XML tags, render them regardless of Description mode or AltDocComment
                Brush brush = (DocTag.MissingEndTag ? WARNING_BRUSH : COMMENT_TAG_BRUSH);
                renderer.RenderText("<" + DocTag.TagName, brush, this);
                if (DocTag.Attributes != null && DocTag.Attributes.Count > 0)
                {
                    foreach (KeyValuePair<string, object> pair in DocTag.Attributes)
                    {
                        renderer.RenderText(" " + pair.Key + "=\"", COMMENT_TAG_BRUSH, this);
                        if (pair.Value is string)
                            renderer.RenderText((string)pair.Value, COMMENT_TAG_BRUSH, this);
                        else
                            CreateVM((CodeObject)pair.Value).Render(renderer, flags);
                        renderer.RenderText("\"", COMMENT_TAG_BRUSH, this);
                    }
                }
                renderer.RenderText(_contentVM == null && !DocTag.MissingEndTag ? "/>" : ">", brush, this);
            }
        }

        protected override void RenderContent(CodeRenderer renderer, TextStyle textStyle, RenderFlags flags)
        {
            // Don't trim newlines for unrecognized tags, since the tags are always rendered
            // (we also have to ignore AltDocComment, so replace the entire base-class method).
            if (_contentVM is string)
                DocTextVM.RenderText(renderer, (string)_contentVM, textStyle, flags, this);
            else if (_contentVM is ChildListVM<DocCommentVM>)
            {
                foreach (DocCommentVM commentVM in (ChildListVM<DocCommentVM>)_contentVM)
                    commentVM.Render(renderer, textStyle, flags);
            }
            else if (_contentVM is CodeObjectVM)
                ((CodeObjectVM)_contentVM).Render(renderer, flags);
        }

        protected override void RenderEnd(CodeRenderer renderer, RenderFlags flags)
        {
            // For unrecognized XML tags, render them regardless of Description mode or AltDocComment
            if (!DocTag.MissingEndTag && (_contentVM != null || DocTag.MissingStartTag))
            {
                string tagName = DocTag.TagName;
                if (tagName != null)
                    renderer.RenderText("</" + tagName + ">", DocTag.MissingStartTag ? WARNING_BRUSH : COMMENT_TAG_BRUSH, this);
            }
        }

        #endregion
    }
}
